package cn.drenal.capcap.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.drenal.capcap.common.Response;
import cn.drenal.capcap.entity.Artwork;
import cn.drenal.capcap.entity.vo.ArtworkListVO;
import cn.drenal.capcap.entity.vo.ArtworkDetailVO;
import cn.drenal.capcap.service.ArtworkService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 作品控制器
 */
@RestController
@RequestMapping("/artwork")
@RequiredArgsConstructor
public class ArtworkController {

    private final ArtworkService artworkService;

    /**
     * 创建作品
     *
     * @param artwork 作品信息
     * @return 创建结果
     */
    @PostMapping
    @SaCheckPermission("content:artwork:add")
    public Response<Artwork> createArtwork(@RequestBody Artwork artwork) {
        return Response.success(artworkService.createArtwork(artwork));
    }

    /**
     * 更新作品
     *
     * @param id 作品ID
     * @param artwork 作品信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    @SaCheckPermission("content:artwork:edit")
    public Response<Artwork> updateArtwork(@PathVariable Long id, @RequestBody Artwork artwork) {
        // 确保ID正确设置
        artwork.setId(id);
        // 处理标签和图片关联
        return Response.success(artworkService.updateArtwork(id, artwork));
    }

    /**
     * 删除作品
     *
     * @param id 作品ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("content:artwork:delete")
    public Response<Void> deleteArtwork(@PathVariable Long id) {
        artworkService.deleteArtwork(id);
        return Response.success();
    }

    /**
     * 根据ID查询作品
     *
     * @param id 作品ID
     * @return 作品信息
     */
    @GetMapping("/{id}")
    public Response<ArtworkDetailVO> getArtworkById(@PathVariable Long id) {
        return Response.success(artworkService.getArtworkDetailById(id));
    }

    /**
     * 分页查询作品列表
     *
     * @param current 当前页
     * @param size 每页大小
     * @param title 标题（可选）
     * @param status 状态（可选）
     * @param categoryId 分类ID（可选）
     * @return 作品分页列表
     */
    @GetMapping("/page")
    @SaCheckPermission("content:artwork:list")
    public Response<Page<ArtworkListVO>> getArtworkPage(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) Long categoryId) {
        Page<ArtworkListVO> page = new Page<>(current, size);
        return Response.success(artworkService.getArtworkPage(page, title, status, categoryId, null));
    }
    
    /**
     * 审核作品
     *
     * @param id 作品ID
     * @param status 审核状态
     * @param rejectReason 拒绝原因（当状态为拒绝时必填）
     * @return 更新后的作品
     */
    @PutMapping("/{id}/review")
    @SaCheckPermission("content:artwork:review")
    public Response<Artwork> reviewArtwork(
            @PathVariable Long id,
            @RequestParam Integer status,
            @RequestParam(required = false) String rejectReason) {
        return Response.success(artworkService.reviewArtwork(id, status, rejectReason));
    }
    
    /**
     * 增加作品浏览量
     *
     * @param id 作品ID
     * @return 操作结果
     */
    @PutMapping("/{id}/view")
    public Response<Void> incrementViewCount(@PathVariable Long id) {
        artworkService.incrementViewCount(id);
        return Response.success();
    }
}